package com.droidblackbox.manager;

import android.app.Activity;
import android.util.DisplayMetrics;

public final class DisplayManager {

	private static DisplayManager instance = null;
	private DisplayMetrics displayMetrics = null;

	private static final float DEFAULT_HDIP_DENSITY_SCALE = 1.5f;

	private float density;
	private float scaledDensity;
	private int densityDpi;
	private int heightPixels;
	private int widthPixels;
	private float xdpi;
	private float ydpi;

	public synchronized static DisplayManager setInstance(Activity activity) {
		if (instance == null) {
			synchronized (DisplayManager.class) {
				if (instance == null) {
					instance = new DisplayManager(activity);
				}
			}
		}

		return getInstance();
	}

	public synchronized static DisplayManager getInstance() {
		if (instance == null)
			return null;
		return instance;
	}

	private DisplayManager(Activity activity) {
		displayMetrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay()
				.getMetrics(displayMetrics);
		density = displayMetrics.density;
		densityDpi = displayMetrics.densityDpi;
		heightPixels = displayMetrics.heightPixels;
		widthPixels = displayMetrics.widthPixels;
		xdpi = displayMetrics.xdpi;
		ydpi = displayMetrics.ydpi;
		scaledDensity = displayMetrics.scaledDensity;

		// System.out.println("density : " + density);
		// System.out.println("densityDpi : " + densityDpi);
		// System.out.println("heightPixels : " + heightPixels);
		// System.out.println("widthPixels : " + widthPixels);
		// System.out.println("xdpi : " + xdpi);
		// System.out.println("ydpi : " + ydpi);
		// System.out.println("scaledDensity : " + scaledDensity);

	}

	/**
	 * 
	 * @return density
	 */
	public float getDensity() {
		return density;
	}

	/**
	 * 
	 * @return scaledDensity
	 */
	public float getScaledDensity() {
		return scaledDensity;
	}

	/**
	 * 
	 * @return densityDpi
	 */
	public int getDensityDpi() {
		return densityDpi;
	}

	/**
	 * 
	 * @return heightPixels
	 */
	public int getHeightPixels() {
		return heightPixels;
	}

	/**
	 * 
	 * @return widthPixels
	 */
	public int getWidthPixels() {
		return widthPixels;
	}

	/**
	 * 
	 * @return xdpi
	 */
	public float getXdpi() {
		return xdpi;
	}

	/**
	 * 
	 * @return ydpi
	 */
	public float getYdpi() {
		return ydpi;
	}

	/**
	 * 
	 * @param pixel
	 *            �쎌�
	 * @return 蹂�솚��媛�(DP)
	 */
	public int getDPfromPixel(int pixel) {
		return (int) (pixel / DEFAULT_HDIP_DENSITY_SCALE * density);
	}

	/**
	 * 
	 * @param dp
	 * @return int pixel
	 */
	public int getPixelfromDP(int dp) {
		return (int) (dp / density * DEFAULT_HDIP_DENSITY_SCALE);
	}

	/**
	 * 
	 * @param pixel
	 * @return DP
	 */
	public float getDPfromPixel(float pixel) {
		return (pixel / DEFAULT_HDIP_DENSITY_SCALE * density);
	}

	/**
	 * 
	 * @param dp
	 * @return float pixel
	 */
	public float getPixelfromDP(float dp) {
		return (dp / density * DEFAULT_HDIP_DENSITY_SCALE);
	}
}
